package com.fancy.learncenter.ui.view.date.custom;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.fancy.learncenter.R;


/**
 * Created by linhuasen on 15/12/25.
 */
public class DateTimePickerDialog extends DialogFragment {

    public static final String TAG = DateTimePickerDialog.class.getSimpleName();

    public OnDateTimePickerListener listener;

    public void setListener(OnDateTimePickerListener listener){
        this.listener = listener;
    }

    public static DateTimePickerDialog getInstance(){
        DateTimePickerDialog fragment = new DateTimePickerDialog();
        return fragment;
    }

    public static DateTimePickerDialog getInstance(String time,String parten ,int type){
        DateTimePickerDialog fragment = new DateTimePickerDialog();
        Bundle args = new Bundle();
        args.putString("time",time);
        args.putString("parten",parten);
        args.putInt("type",type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        return super.onCreateDialog(savedInstanceState);

        Bundle args = getArguments();
        int type = args.getInt("type", 0);
        String time = args.getString("time");
        String parten = args.getString("parten");

        Dialog dialog = new Dialog(getActivity(), R.style.DateTimePickDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_date_time_picker,null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);

        final DateTimePicker picker = (DateTimePicker) view.findViewById(R.id.picker);
        if(!TextUtils.isEmpty(time)){
            picker.setTime(time,parten);
        }
        picker.configDateTimePicker(type);

        final TextView txvCancel = (TextView) view.findViewById(R.id.txv_cancel);
        txvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DateTimePickerDialog.this.dismiss();
            }
        });
        final TextView txvSure = (TextView) view.findViewById(R.id.txv_sure);
        txvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time = picker.getTextStr();
                Log.d("lhs",time);
                if(listener != null){
                    listener.onDateTimePicked(picker.getTime());
                }
                DateTimePickerDialog.this.dismiss();
            }
        });


        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = getScreenHeight(getContext()) *2/5;
        window.setAttributes(wlp);

        return dialog;
    }

    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    public interface OnDateTimePickerListener{
        public void onDateTimePicked(long time);
    }
}
